package com.itheima.cs.common.wwdb;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.itheima.cs.R;
import com.itheima.cs.common.CustomCaptureActivity;
import com.itheima.cs.dao.MaterialDao;
import com.itheima.cs.entity.dyewm.MaterialInfo;

public class OutsourcingAllocationActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_add;

    private EditText editTextSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outsourcing_allocation);
        initView();
    }

    private void initView() {
        btn_add = findViewById(R.id.btn_add);

        btn_add.setOnClickListener(this);

        editTextSearch = findViewById(R.id.editText_search);

        editTextSearch.requestFocus();

        editTextSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String input = s.toString();
                if (!TextUtils.isEmpty(input)) {
                    handleScanResult(input);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        Toolbar toolbar = findViewById(R.id.toolbar_outsourcing_allocation);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){


            case R.id.btn_add:{
                doScan();
                break;
            }


        }
    }

    private void doScan(){
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.setCaptureActivity(CustomCaptureActivity.class);
        intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
        intentIntegrator.setPrompt("请对准二维码");
        intentIntegrator.setCameraId(0);
        intentIntegrator.setBeepEnabled(false);
        intentIntegrator.setBarcodeImageEnabled(false);
        intentIntegrator.setOrientationLocked(true);
        intentIntegrator.initiateScan();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            handleScanResult(result);
        }

    }

    private void handleScanResult(IntentResult result) {
        if (result != null && result.getContents() != null) {
            handleScanResult(result.getContents());
        }
    }

    private void handleScanResult(String rawInput) {
        if (TextUtils.isEmpty(rawInput)) return;

        String materialCode = rawInput.trim();
        if (materialCode.isEmpty()) return;
        new MaterialQueryTask().execute(materialCode);
    }

    private class MaterialQueryTask extends AsyncTask<String, Void, MaterialInfo> {
        private String inputCode;

        @Override
        protected MaterialInfo doInBackground(String... params) {
            inputCode = params[0];
            MaterialDao dao = new MaterialDao();
            return dao.getMaterialByCode(inputCode);
        }

        @Override
        protected void onPostExecute(MaterialInfo materialInfo) {
            if (materialInfo != null && !TextUtils.isEmpty(materialInfo.getCode())) {
                Intent intent = new Intent(OutsourcingAllocationActivity.this, OutsourcingAllocationFormActivity.class);
                intent.putExtra("code", materialInfo.getCode());
                intent.putExtra("name", materialInfo.getName());
                intent.putExtra("spec", materialInfo.getSpec());
                startActivity(intent);
            } else {
                String errorMessage = "编码不存在或二维码格式错误：" + inputCode;
                Toast.makeText(OutsourcingAllocationActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                editTextSearch.setText("");
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        editTextSearch.setText("");
    }
}

